package com.star.carserviceapi.domain.result.base;

import com.star.carserviceapi.domain.request.base.BaseRequest;
import com.star.carserviceapi.utils.HttpApiUtils;

public abstract class BaseResult<T> {
	private int code = -1;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	private T data;

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getMessage() {

		if (error != null) {
			return error.getMessage();
		}

		return "";
	}

	public void flagSuccess() {
		this.code = 200;
	}

	public boolean successful() {

		if (error != null) {
			return false;
		}

		return this.code == 200;
	}

	private BaseErrorInfo error;

	public BaseErrorInfo getError() {
		return error;
	}

	public void setError(BaseErrorInfo error) {
		this.error = error;
	}

	@SuppressWarnings("unchecked")
	public <T2 extends BaseResult<T>> T2 invoke(BaseRequest input) {

		T2 result = (T2) HttpApiUtils.invokeServer(input, this);

		result.packing(result.getData());

		return result;
	}

	protected void packing(T data) {

	}
}
